package com.googlesource.gerrit.plugins.examples.servlet.export;

/**
 * @author zxs
 * @date 2022/5/9
 */
public class ChangeSizeBucket {

    /**
     * The upper bounds for the different size buckets.
     *
     * <p>
     * Same as gr-change-list-item.ts::ChangeSize.
     */
    private enum BucketThresholds {
        XS(10),
        SMALL(50),
        MEDIUM(250),
        LARGE(1000);

        public final long delta;

        BucketThresholds(long delta) {
            this.delta = delta;
        }
    }

    /**
     * Gets the correlative size bucket for the given change delta.
     *
     * <p>
     * Same as gr-change-list-item.ts::computeChangeSize().
     *
     * @param delta the total number of changed lines (additions+deletions) of the
     *              change.
     * @return a short human-readable size bucket.
     */
    public static String getChangeSizeBucket(long delta) {
        if (delta == 0) {
            return "NoOp";
        } else if (delta < BucketThresholds.XS.delta) {
            return "XS";
        } else if (delta < BucketThresholds.SMALL.delta) {
            return "S";
        } else if (delta < BucketThresholds.MEDIUM.delta) {
            return "M";
        } else if (delta < BucketThresholds.LARGE.delta) {
            return "L";
        }
        return "XL";
    }

}
